package com.mk.settlement.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.mk.settlement.enums.SettlementStateEnums;
import com.mk.settlement.enums.SettlementTypeEnums;
import com.mk.settlement.mappers.SettlementOrderMapper;
import com.mk.settlement.mappers.SettlementResultMapper;
import com.mk.settlement.service.SettlementRegularService;

@Service(value="settlementMonthService")
public class SettlementMonthService implements SettlementRegularService {

    private Logger log = LoggerFactory.getLogger(SettlementMonthService.class);

    @Resource
    private SettlementResultMapper settlementResultMapper;

    @Resource
    private SettlementOrderMapper settlementOrderMapper;
    
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 00:00:00");

    @Override
    public void doSettlement() {

        log.info("月结定时worker开始执行...");

        try {
            settlementResultMapper.insertNotStatisticsBySType(SettlementTypeEnums.MONTH.getIndex(), sdf.format(new Date()));
        } catch (Exception e) {

            log.error("月结定时worker执行过程异常!", e);
            
            throw e;
        }

        log.info("月结定时worker执行完毕.");

        log.info("更新已统计订单数据状态...");
        int r;
        try {
            r = settlementOrderMapper.updateSettlementState(SettlementStateEnums.NotStatistics.getIndex(),
                    SettlementStateEnums.AlreadyStatistics.getIndex(), SettlementTypeEnums.MONTH.getIndex(), sdf.format(new Date()), sdf.format(new Date()));
        } catch (Exception e) {
            log.info("更新已统计订单数据状态异常!", e);
            
            throw e;
        }

        log.info("更新已统计订单数据状态完毕,共更新[" + r + "]条." );
    }

}
